
//
//  RunLoopViewController.m
//  DebugDemo
//
//  Created by XFB on 2018/11/28.
//  Copyright © 2018年 ZhouCheng. All rights reserved.
//

#import "RunLoopViewController.h"
#import "AmountModel.h"
#import "AmountCell.h"
#import "AmountHeaderView.h"

@interface RunLoopViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray *dataSource;
@property (nonatomic, strong) UITableView *tableView;

@end

@implementation RunLoopViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self test1];
    
    self.navigationItem.title = @"RunLoop解析";
    self.view.backgroundColor = [UIColor whiteColor];
    [self setupUI];
}

- (void)test1
{
    /// 工厂模式的好处
    /**
     什么时候使用工厂模式：
     1.编译时无法确定预期要创建的对象类
     */
    
}

- (void)setupUI
{
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[AmountCell class] forCellReuseIdentifier:@"AmountCell"];
}

#pragma mark --- <UITableViewDataSource>

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

#pragma mark --- <UITableViewDelegate>
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AmountCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AmountCell"];
    AmountModel *amountM = self.dataSource[indexPath.section];
    cell.amountModel = amountM;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AmountModel *amountM = self.dataSource[indexPath.section];
    return amountM.cellHeight;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    AmountHeaderView *headerView = [AmountHeaderView amountHeaderViewViewWithTableView:tableView];
    AmountModel *amountM = self.dataSource[section];
    headerView.amountModel = amountM;
    return headerView;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 60;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}

#pragma mark --- <Lazy loading>
- (NSArray *)dataSource
{
    if (_dataSource == nil) {
        NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"Amount.plist" ofType:nil];
        NSArray *plistArray = [NSArray arrayWithContentsOfFile:plistPath];
        NSArray *modelArray = [AmountModel mj_objectArrayWithKeyValuesArray:plistArray];
        _dataSource = [modelArray mutableCopy];
    }
    return _dataSource;
}

- (UITableView *)tableView
{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, NavBarHeight, ScreenW, ScreenH-NavBarHeight-BottomHeight) style:UITableViewStyleGrouped];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = RGB(250, 250, 250);
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}

@end

